package v1

import (
	"bc/dao"
	"bc/service"
	"bc/util"
	"github.com/gin-gonic/gin"
)

func LogAdd(c *gin.Context,con string, logType string){

	uname,bool := c.Get("username")
	uid := c.GetInt("uid")
	if !bool{
		uname = c.ClientIP()
	}
	ip := c.ClientIP()
	header := c.Request.Header.Get("User-agent")
	service.LogService.Add(uid, uname.(string), con, ip, header, logType)

}

func LogList(c *gin.Context)  {
	data := dao.LogDao
	var err error
	var pageSize = 10
	var pageIndex = 1

	size := c.Request.FormValue("pageSize")
	if size != "" {
		pageSize = util.StrToInt(err, size)
	}

	index := c.Request.FormValue("pageIndex")
	if index != "" {
		pageIndex = util.StrToInt(err, index)
	}

	startTime := c.Request.FormValue("start_time")
	st := 0
	if startTime != "" {
		//st = util.StrToInt(err, startTime)
		st = util.TimeToTimestamp(startTime)
	}
	stopTime := c.Request.FormValue("stop_time")
	stm := 0
	if stopTime != "" {
		//stm = util.StrToInt(err, stopTime)
		stm = util.TimeToTimestamp(stopTime)
	}

	content := c.Request.FormValue("content")
	if content != "" {
		data.Content = content
	}

	result, count, err := data.List(pageIndex, pageSize, st, stm)
	util.HasError(c, err, "", -1)

	var mp = make(map[string]interface{}, 3)
	mp["list"] = result
	mp["count"] = count
	mp["pageIndex"] = pageIndex
	mp["pageSize"] = pageSize
	util.PageOK(c, result, count, pageIndex, pageSize, "")
}
